package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IProductService;
import com.wondertek.poms.dao.po.Product;
import com.wondertek.poms.dao.pojo.ProductPojo;
import com.wondertek.poms.service.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Api(value = "ProductController", description = "产品计费点管理")
@RestController
@RequestMapping(value="/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @ApiOperation("产品计费点 列表")
    @GetMapping(value = "/list")
    @ResponseBody
    public ResultBean list(ProductVo productVo,
                          @RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "20") int rows,
                          @RequestParam(defaultValue = "updateTime") String sidx,
                          @RequestParam(defaultValue = "desc") String sord){
        ProductPojo productpojo=new ProductPojo();
        BeanUtils.copyProperties(productVo,productpojo);
        Page<ProductPojo> list=productService.findAll(productpojo,page,rows,sord,sidx);
        return PageBean.ok(list.getTotalPages(),list.getTotalElements(),list.getContent());
    }

    @ApiOperation("产品计费点 添加")
    @PostMapping(value = "/add")
    @ResponseBody
    public ResultBean list(@RequestBody ProductVo productVo){
        List<String> messageList = ValidateFilter.getFilterMessage(productVo, Constants.VALIDATE_DEFAULT);
        if (!messageList.isEmpty()) {
            return ResultBean.error(messageList.toString());
        }
        if(isRepeat(productVo)){
            return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
        }

        Product product=new Product();
        BeanUtils.copyProperties(productVo,product);
        Product.putTimeAndUser(product);
        productService.save(product);
        return ResultBean.ok(product);
    }


    public boolean isRepeat(ProductVo productVo){
        Product temp=new Product();
        temp.setProductID(productVo.getProductID());
        Optional<Product> productOpt=productService.findOne(Example.of(temp));
        if(productOpt.isPresent()){
            Product product=productOpt.get();
            if (productVo.getProductID() != null && productVo.getProductID() != product.getProductID())
                return true;
            else if (productVo.getProductID() == null)
                return true;
        }
        return false;
    }

}
